<?php

class FormBuilder {
	/* Example usage: {

		$elements = array(	array('form_start'),
							array('hidden_instance'),
							array('hidden_action', 'value'=>'edit'),
							array('hidden_new'),
							array('custom', 'data'=>$heading),
							array('custom', 'data'=>$link),
							array('textarea', 'name'=>'text', 'text'=>$row['text']),
							array('footer_start'),
							array('custom', 'data'=>$message),
							array('submit', 'value'=>'Save'),
							array('footer_end'),
							array('form_end'));
							
		$form = $this->generateForm($elements);
		
	}
	*/
	public static function generateForm($elements=array(), $moduleName, $instanceName) {
		$form = '';
		foreach($elements as $data) {
			switch($data[0]) {
				
			case "form_start":
				if(!isset($data['method'])) { $data['method'] = 'post'; }
 				$form .= '<form id="citru_'.$moduleName.'_'.$instanceName.'" action="" method="'.$data['method'].'" enctype="'.$data['enctype'].'">'."\n";
				break;
			
			case "hidden_instance":
				$form .= '<input type="hidden" name="instance" value="'.$moduleName.'_'.$instanceName.'" />'."\n";
				break;
			
			case "hidden_action":
				$form .= '<input type="hidden" name="action" value="'.$data['value'].'" />'."\n";
				break;
			
			case "hidden_new":
				$form .= '<input type="hidden" name="new" value="'.$data['value'].'" />'."\n";
				break;
			
			case "hidden_custom":
				$form .= '<input type="hidden" name="'.$data['name'].'" value="'.$data['value'].'" />'."\n";
				break;
			
			case "label":
				$form .= '<label class="citru_label '.$data['class'].'" for="citru_'.$moduleName.'_'.$instanceName.'_'.$data['for'].'">'.$data['text'].'</label>'.$data['after']."\n";
				break;
			
			case "input_text":
				$form .= '<input class="citru_input_text '.$data['class'].'" id="citru_'.$moduleName.'_'.$instanceName.'_'.$data['name'].'" name="'.$data['name'].'" type="text" value="'.$data['value'].'" />'.$data['after']."\n";
				break;
			
			case "input_password":
				$form .= '<input class="citru_input_text citru_input_password '.$data['class'].'" id="citru_'.$moduleName.'_'.$instanceName.'_'.$data['name'].'" name="'.$data['name'].'" type="password" value="'.$data['value'].'" />'.$data['after']."\n";
				break;
			
			case "input_checkbox":
				$form .= '<p class="citru_input_checkbox '.$data['class'].'"><input id="citru_'.$moduleName.'_'.$instanceName.'_'.$data['name'].'" name="'.$data['name'].'" type="checkbox" '.$data['checked'].' />'.$data['text'].'</p>'.$data['after']."\n";
				break;
			
			case "input_select":
				$form .= '<select class="citru_input_select '.$data['class'].'" id="citru_'.$moduleName.'_'.$instanceName.'_'.$data['name'].'"  name="'.$data['name'].'">'."\n";
				foreach($data['options'] as $i=>$option) {
					if(intval($data['selected']) == $i) {
						$selected = ' selected';
					} else {
						$selected = '';
					}
					$form .= "\t".'<option value="'.$data['values'][$i].'"'.$selected.'>'.$option.'</option>'."\n";
				}
				$form .= '</select>'.$data['after']."\n";
				
				break;
			
			case "input_file":
				$form .= '<input class="citru_input_file '.$data['class'].'" id="citru_'.$moduleName.'_'.$instanceName.'_'.$data['name'].'" name="'.$data['name'].'" type="file" />'.$data['after']."\n";
				break;
			
			case "textarea":
				$form .= '<textarea class="citru_input_textarea" id="citru_'.$moduleName.'_'.$instanceName.'_'.$data['name'].'" name="'.$data['name'].'">'.$data['text'].'</textarea>'.$data['after']."\n";
				break;
			
			case "fieldset_start":
				$form .= '<fieldset><legend>'.$data['legend'].'</legend>'.$data['after']."\n";
				break;
			
			case "fieldset_end":
				$form .= '</fieldset>'."\n";
				break;
			
			case "footer_start":
				$form .= '<div class="citru_module_footer">'."\n";
				break;
			
			case "footer_end":
				$form .= '</div>'."\n";
				break;
			
			case "submit":
				if($data['order'] == 'reversed') {
					$form .= '<button class="citru_submit citru_reversed citru_button_positive" id="citru_'.$moduleName.'_'.$instanceName.'_submit" type="submit">'.$data['value'].'<img src="inc/icons/'.$data['icon'].'.png" /></button>'.$data['after']."\n";
				} else {
					$form .= '<button class="citru_submit citru_button_positive" id="citru_'.$moduleName.'_'.$instanceName.'_submit" type="submit"><img src="'.ROOT.'inc/icons/'.$data['icon'].'.png" />'.$data['value'].'</button>'.$data['after']."\n";
				}
				break;

			case "p":
				$form .= '<p class="'.$data['class'].'">'.$data['text'].'</p>'.$data['after']."\n";
				break;
			
			case "custom":
				$form .= $data['data'];
				break;
			
			case "form_end":
				$form .= '</form>';
				break;
			}
		}
		return $form;
	}
	
}

?>
